package com.xhu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhu.common.ResponseInfo;

import java.util.HashMap;
import java.util.Map;

import static com.xhu.constant.ResultConstant.SUCCESS;

public class MyBaseController {
    protected String returnPages(IPage<?> result) {
        ResponseInfo<Map<String, Object>> response = new ResponseInfo<>();

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("pageCount", result.getPages());
        resMap.put("content", result.getRecords());

        response.setStatus(SUCCESS);
        response.setData(resMap);

        return JSON.toJSONString(response);
    }

    protected String returnSuccessWithNoData(String message) {
        ResponseInfo<Void> res = new ResponseInfo<Void>()
                .setMsg(message)
                .setStatus(SUCCESS);
        return JSON.toJSONString(res);
    }

    protected String getSingleStr(String content, String key) {
        return JSON.parseObject(content, Map.class).get(key).toString();
    }


    protected <T> String returnSuccessWithData(String message, T data) {
        ResponseInfo<T> res = new ResponseInfo<T>()
                .setMsg(message)
                .setStatus(SUCCESS);
        res.setData(data);
        return JSON.toJSONString(res);
    }


}
